//初始化路由数据
export function initializeRouter(responseRoutes) {
  let baseMenu = {
    path: "/",
    component: require("@/views/layout/Layout").default,
    redirect: "/home",
    name: "home",
    hidden: true,
    children: [
      {
        path: "home",
        name: "home",
        component: () => import("@/views/home/index")
      },
      {
        path: "personalInfo",
        name: "personalInfo",
        component: () => import("@/views/myInfor/personalInfo/index")
      },
      {
        path: "editPassword",
        name: "editPassword",
        component: () => import("@/views/myInfor/editPassword/index")
      },
      {
        path: "completeUserInfo",
        name: "completeUserInfo",
        component: () => import("@/views/myInfor/completeUserInfo/index"),
      }
    ]
  };
  let newRoutes = [];
  newRoutes.push(baseMenu);
  console.log(responseRoutes);
  for (let route of responseRoutes) {
    // urlPart1:一级路由名称以及文件夹名称,urlPart2:一级路由重定向默认子级路由
    let urlPart1 = "";
    let urlPart2 = "";
    try {
      urlPart1 = (route.child[0].route.split("/"))[0];
      urlPart2 = (route.child[0].route.split("/"))[1];
    } catch (e) {
      console.log(e);
      console.log("%c解析错误路由:","background:red;color:#fff",route.child[0]);
      continue
    }
    let obj1 = {
      path: "/" + urlPart1,
      name: urlPart1,
      component: require("@/views/layout/Layout").default,
      redirect: "/" + urlPart1 + "/" + urlPart2,
      meta: { title: route.menu_name, icon: route.icon }
    };
    if (route.child && route.child.length > 0) {
      obj1.children = [];
      for (let subRoute of route.child) {
        if (subRoute.route) {
          //urlPart3:二级路由名称以及对应文件夹路径，urlPart5:二级路由对应文件夹下文件名称
          let urlPart3 = subRoute.route.split("/")[1];
          let urlPart4 = subRoute.route.split("/")[2];
          try {
            const component = require(`@/views/${urlPart1}/${urlPart3}/${urlPart4}`);
            let obj2 = {
              path: urlPart3 + "/" + urlPart4,
              name: urlPart3 + "_" + urlPart4,
              hidden: subRoute.is_show == 1 ? false : true,
              component: component.default,
              meta: { title: subRoute.menu_name }
            };
            obj1.children.push(obj2);
          } catch (e) {
            console.log(e);
          }

        }
      }
    }
    newRoutes.push(obj1);
  }
  // 未找到页面 跳转404页
  let notfound = { path: "*", redirect: "/404", hidden: true };
  newRoutes.push(notfound);
  return newRoutes;
}

// 防抖
export function debounce(func, wait, immediate) {
  let timeout, args, context, timestamp, result;

  const later = function () {
    // 据上一次触发时间间隔
    const last = +new Date() - timestamp;

    // 上次被包装函数被调用时间间隔last小于设定时间间隔wait
    if (last < wait && last > 0) {
      timeout = setTimeout(later, wait - last);
    } else {
      timeout = null;
      // 如果设定为immediate===true，因为开始边界已经调用过了此处无需调用
      if (!immediate) {
        result = func.apply(context, args);
        if (!timeout) context = args = null;
      }
    }
  };

  return function (...args) {
    context = this;
    timestamp = +new Date();
    const callNow = immediate && !timeout;
    // 如果延时不存在，重新设定延时
    if (!timeout) timeout = setTimeout(later, wait);
    if (callNow) {
      result = func.apply(context, args);
      context = args = null;
    }

    return result;
  };
}

